<?php
class SF_CustomerExtension_CustomerController extends Mage_Core_Controller_Front_Action
{
	/*判断是否登录*/
	public function preDispatch()
    {
       	parent::preDispatch();
 
       	if (!Mage::getSingleton('customer/session')->authenticate($this)) 
       	{
            $this->setFlag('', 'no-dispatch', true);
        }
    }
		
    public function printinvoicepdfAction() {
        $orderId = (int) $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($orderId);
        if ($this->_canViewOrder($order)) {
			if ($order->hasInvoices()) {
				foreach ($order->getInvoiceCollection() as $invoice) {
					if($invoiceIncrementID = $invoice->getIncrementId()) break;
				}
				
				$_invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementID);
				if ($_invoice) {
					$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($_invoice));
					return $this->_prepareDownloadResponse('invoice_'.$order->getIncrementId().
							'_'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
							'.pdf', $pdf->render(), 'application/pdf');
				}
			}
        }
    }
    
    protected function _canViewOrder($order)
    {
        $customerId = Mage::getSingleton('customer/session')->getCustomerId();
        $availableStates = Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates();
        if ($order->getId() && $order->getCustomerId() && ($order->getCustomerId() == $customerId)
            && in_array($order->getState(), $availableStates, $strict = true)
            ) {
                return true;
            }
            return false;
    }
}
